package ru.mcfr.oxygen.framework.operations.table;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import ro.sync.ecss.extensions.api.ArgumentDescriptor;
import ro.sync.ecss.extensions.api.AuthorOperationException;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;
import ru.mcfr.oxygen.framework.operations.McfrBaseAuthorOperation;
import ru.mcfr.oxygen.framework.utils.XmlTool;

import javax.swing.text.BadLocationException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: wstarcev
 * Date: 12.07.11
 * Time: 16:45
 * To change this template use File | Settings | File Templates.
 */
public class InsertColumnOperation extends McfrBaseAuthorOperation{

    {
        description = "Вставка столбца в таблицу";

        argumentsNameArray = new String[]{"направление"};

        argumentsTypesArray = new Object[]{ArgumentDescriptor.TYPE_STRING};

        argumentsDescriptionsArray = new String[]{"Направление вставки от выбранного столбца: справа/слева"};
    }

    @Override
    public void doMainOperation() {
        AuthorNode mainElement = selectedElement;
        while (!mainElement.getName().equals("ячейка"))
            mainElement = mainElement.getParent();

        String id = ((AuthorElement) mainElement).getAttribute("id").getValue();

        AuthorElement root = controller.getAuthorDocumentNode().getRootElement();
        try {
            String stringFragment = controller.serializeFragmentToXML(controller.createDocumentFragment(root, true));
            stringFragment = "<?xml version='1.0'?>" + stringFragment;
            authorAccess.getWorkspaceAccess().showErrorMessage(stringFragment);
            Document XMLDoc = XmlTool.stringXMLtoDocument(stringFragment);
            Element element = XMLDoc.getElementById(id);
            String parentID = ((Element)element.getParentNode()).getAttribute("id");
            authorAccess.getWorkspaceAccess().showErrorMessage("Cell id:" + id + "; Parent id:" + parentID);
        } catch (BadLocationException e) {
            authorAccess.getWorkspaceAccess().showErrorMessage("Bad allocation: " + e.getMessage());
        } catch (ParserConfigurationException e) {
            authorAccess.getWorkspaceAccess().showErrorMessage("Bad allocation: " + e.getMessage());
        } catch (SAXException e) {
            authorAccess.getWorkspaceAccess().showErrorMessage("Bad allocation: " + e.getMessage());
        } catch (IOException e) {
            authorAccess.getWorkspaceAccess().showErrorMessage("Bad allocation: " + e.getMessage());
        } catch (Exception e){
            authorAccess.getWorkspaceAccess().showErrorMessage("Unknown exception");
        }

    }

    @Override
    public void doOperationWithSelected() throws AuthorOperationException, BadLocationException {

    }

    @Override
    public void doOperationAtCaretPos() throws AuthorOperationException {

    }
}
